package com.caisejingyu.whales.smartrotob.main;

import android.os.Build;

import com.caisejingyu.whales.common.AppConfig;
import com.caisejingyu.whales.common.BaseApplication;
import com.caisejingyu.whales.common.api.ApiConfig;
import com.caisejingyu.whales.common.utils.CrashHelper;
import com.caisejingyu.whales.common.utils.DeviceUtil;
import com.caisejingyu.whales.control.toast.ToastUtil;
import com.joyoung.main.BuildConfig;
import com.caisejingyu.whales.toastimpl.ToastBuilderImpl;

import java.util.UUID;

import static com.caisejingyu.whales.common.NetworkEnvironment.ENV_PRE;
import static com.caisejingyu.whales.common.NetworkEnvironment.ENV_PRODUCT;
import static com.caisejingyu.whales.common.NetworkEnvironment.ENV_TEST;

public abstract class MainApp extends BaseApplication {
    
    @Override
    public void onCreate() {
        super.onCreate();
        defaultInit();
    }
    
    @Override
    public void init() {
        try {
//            ServiceManagerFactory.getInstance().put(IHomeApi.class, HomeApiImpl.class);
//            ServiceManagerFactory.getInstance().put(IMenuApi.class, SettingApiImpl.class);
//            ServiceManagerFactory.getInstance().put(IImage.class, ImageImpl.class);
        } catch (Throwable e) {
            e.printStackTrace();
        }
        
        CrashHelper.getInstance().init(this);
        ToastUtil.setToastBuilderProxyClass(ToastBuilderImpl.class);
        AppConfig config = new AppConfig.Builder()
                .isDebugMode(com.caisejingyu.whales.common.BuildConfig.DEBUG)
                .isLogEnabled(BuildConfig.DEBUG)
                .appType("1")
                .appName("Android")
                .appVersion(DeviceUtil.getVersionName(this))
                .setModel(Build.MANUFACTURER + Build.MODEL)
                .setSystemName("\"Android\"" + Build.VERSION.RELEASE)
                .setSystemVersion(String.valueOf(Build.VERSION.SDK_INT))
                .setMobileId(UUID.randomUUID().toString())
                .setMobileName(Build.MANUFACTURER)
                .build();
        
        AppConfig.initialize(config);
        initApiConfig();

        onInit();
    }
    
    protected abstract void onInit();

    public void initApiConfig() {
        switch (getAppEnv()) {
            
            case ENV_PRODUCT:
                String mDecryptSecret = "123";
                ApiConfig.init(new ApiConfig.Builder()
                        .baseUrl(ApiConstants.BASE_URL)
                        .appId(ApiConstants.APP_ID)
                        .appSecret(mDecryptSecret)
                        .addUrl(ApiConstants.SET_OPEN_API_KEY, ApiConstants.OPEN_API_URL));
                break;

            case ENV_TEST:
                ApiConfig.init(new ApiConfig.Builder()
                        .baseUrl(ApiConstants.BASE_URL_TEST)
                        .appId(ApiConstants.APP_ID_TEST)
                        .appSecret(ApiConstants.APP_SECRET_TEST)
                        .addUrl(ApiConstants.SET_OPEN_API_KEY, ApiConstants.OPEN_API_URL_TEST));
                break;
            case ENV_PRE:
                ApiConfig.init(new ApiConfig.Builder()
                        .baseUrl(ApiConstants.BASE_URL_TEST)
                        .appId(ApiConstants.APP_ID_TEST)
                        .appSecret(ApiConstants.APP_SECRET_TEST)
                        .addUrl(ApiConstants.SET_OPEN_API_KEY, ApiConstants.OPEN_API_URL_TEST));
                break;
        }
    }

}
